#include "mytestmodel.h"
#include <QVariant>
#include <QColor>
#include <QFile>
#include <QTextStream>
#include <QLatin1String>

#define ENGLISH  "<source>"
#define ENGLISH2 "</source>"
#define CHINESE   "<translation>"
#define CHINESE2   "</translation>"
#define FENGEF    "&*"

MyTestModel::MyTestModel()
    :iRow(400),iCol(2)
{
	 QFile file("./dedibot_zh.ts");
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
          return;

	  QTextStream out(&file);


	  while(!out.atEnd())
	  {
		 QString linetext = out.readLine();
		 if(linetext.contains((ENGLISH2)))
		 {
			// linetext.replace(QLatin1String(ENGLISH), QLatin1String(""), Qt::CaseSensitive);
			//  linetext.replace(QLatin1String(ENGLISH2), QLatin1String(""), Qt::CaseSensitive);
			 int star1 = linetext.indexOf('<');
			 int end1 = linetext.indexOf('>');	

			 linetext.replace(star1, end1 - star1+1, "");

			  int star2 = linetext.indexOf('<');
			   int end2 = linetext.indexOf('>');

			  linetext.replace(star2, end2 - star2 +1, "");

			  linetext =linetext.trimmed();
			 alllist.append(linetext);
			 alllist.append(FENGEF);
		 }
		 else if(linetext.contains((CHINESE2)))
		 {
			 int star1 = linetext.indexOf('<');
			 int end1 = linetext.indexOf('>');	

			 linetext.replace(star1, end1 - star1+1, "");

			  int star2 = linetext.indexOf('<');
			   int end2 = linetext.indexOf('>');

			  linetext.replace(star2, end2 - star2 +1, "");

			   linetext =linetext.trimmed();

			   if(linetext == "")
			   {
				    alllist = alllist.left(alllist.size() - 2);
					int index  = alllist.lastIndexOf(FENGEF);
					 alllist = alllist.left(index + 2);
			   }
			   else
			   {
					alllist.append(linetext);
					alllist.append(FENGEF);
			   }
			
		 }			

	  }
	  alllist.truncate(alllist.size() - 2);

	  allcontext =  alllist.split(FENGEF);

	  iRow = allcontext.size() / 2;

	 
	
	  
	 
	

}

QVariant MyTestModel::data(const QModelIndex &index, int role) const
{
    switch(role)
    {
    case Qt::DisplayRole:
		{
			
		int col = index.column();
		int row = index.row();
		return QVariant(allcontext.at(row * 2 + col));
		}
      //  return QVariant(QString(tr("String%1")).arg((index.column() + 1) * 1000 + index.row() + 1));

    case Qt::ForegroundRole:
        switch(index.column() % 3)
        {
        case 0:
            return QVariant(QColor(Qt::red));
        case 1:
            return QVariant(QColor(Qt::green));
        case 2:
            return QVariant(QColor(Qt::blue));
        default://only to disable warning
            return QVariant(QColor(Qt::white));
        }

        break;
    default:
        return QVariant();
    }
}


int MyTestModel::rowCount(const QModelIndex & /*parent*/) const
{
        return iRow;
}


int MyTestModel::columnCount(const QModelIndex & /*parent*/) const
{
        return iCol;
}

void MyTestModel::setMyData(int idata)
{
    iRow = idata;
    //emit dataChanged(createIndex(0,0), createIndex(iRow,iCol));
    reset();
}
